<?php
# http://projects:8081/cdapp-2kt-api/api/flow_ntfm_get_airports.php
# http://projects:8081/cdapp-2kt-api/api/flow_ntfm_get_airports.php?full=1
# http://projects:8081/cdapp-2kt-api/api/flow_ntfm_get_airports.php?raw=1&nil=1
# http://projects:8081/cdapp-2kt-api/api/flow_ntfm_get_airports.php?nil=1
# http://projects:8081/cdapp-2kt-api/api/flow_ntfm_get_airports.php?nil=1&tnr=1

include_once '_portal.php';
include_once 'flow_ntfm_load_airports_request_contents.php';

const DEF_HOUR_BEF = 4;
const DEF_HOUR_AFT = 20;
const FAKE = false;
const TABLE_NAME = '2kt_flow_record_airports';

$timestamp = timestamp();
$ip = get_client_ip();
$fake = $ip == '127.0.0.1' ? FAKE : false;

$begTime = isset($_GET['begTime']) ? $_GET['begTime'] : date('Y-m-d H', time() - 3600 * DEF_HOUR_BEF) . ':00:00';
$endTime = isset($_GET['endTime']) ? $_GET['endTime'] : date('Y-m-d H', time() + 3600 * DEF_HOUR_AFT) . ':00:00';

$full = isset($_GET['full']) ? $_GET['full'] : false;
$with_tags = isset($_GET['tag']) ? $_GET['tag'] : false;
$with_rates = isset($_GET['tnr']) ? $_GET['tnr'] : false;
$with_history = isset($_GET['his']) ? $_GET['his'] : false;
$with_details = isset($_GET['ext']) ? $_GET['ext'] : false;
$with_raw = isset($_GET['raw']) ? $_GET['raw'] : false;
$without_content = isset($_GET['nil']) ? $_GET['nil'] : false;

$moment = $begTime;

# output $fake data for testing purposes

if ($fake) {
  $begHour = (int)substr($begTime, 11, 2);
  $time_length = strtotime($endTime) - strtotime($begTime);
  $begTime = ($begHour < 15 ? '2022-08-30 ' : '2022-08-29 ') . substr($begTime, 11, 8);
  $endTime = date('Y-m-d H:i:s', strtotime($begTime) + $time_length);
}

$airports = [];
$tags = [];

$payload = param('NTFM_AP_PAYLOAD');
if (($full || $with_tags || $with_rates || $with_details)) {
  foreach ($payload as $key => $value) {
    $airports[] = $key;
    if ($full || $with_tags) {
      $tags_item = explode(':', $value['key']);
      foreach ($tags_item as $tag) {
        $tags[] = $tag;
      }
    }
  }
}

$table_name = TABLE_NAME;
$raw = [];
$length = 0;
foreach ($airports as $item) {
  $sql = "SELECT * FROM `{$table_name}` WHERE `moment` >= '{$begTime}' AND `moment` < '{$endTime}' AND `item`= '{$item}' ORDER BY `moment` ASC";
  $res = exec_sql($sql);
  if ($res && count($res) > 0) {
    $raw[$item] = query_parse($res);
    if ($length == 0) {
      $length = count($res);
    } elseif ($length != count($res)) {
      $length = -1;
    }
  } else {
    $raw[$item] = null;
  }
}

$content = [];
$history = [];
$rates = [];
$details = [];
if ($length > 0) {
  for ($i = 0; $i < $length; $i++) {
    $centent_line = [];
    $history_line = [];
    $rates_line = [];
    $details_line = [];
    foreach ($airports as $item) {
      foreach ($raw[$item][$i]['content'] as $cell) {
        $centent_line[] = $cell;
      }
      foreach ($raw[$item][$i]['history'] as $cell) {
        $history_line[] = $cell;
      }
      $rates_line[] = [
        $raw[$item][$i]['dailyTNR'],
        $raw[$item][$i]['hourlyTNR'],
      ];
      $details_line = $raw[$item][$i]['normalRates'];
    }
    $content[] = $centent_line;
    $history[] = $history_line;
    $rates[] = $rates_line;
    $details[] = $details_line;
  }
}

# version added @2022/12/06
// const PARAM_NAME_VERSION = param('NTFM_AP_TIMESTAMP_RECIVE');
$version = param('NTFM_AP_TIMESTAMP_RECIVE');
if ($fake) {
  $version = date('Y-m-d H:i:s');
}

# airportFlowLimits @2024/01/02

$limits = get_param('airportFlowLimits');

$output = [];
$output['version'] = $version;
$output['reference'] = date('Y-m-d H:i:s');
$output['timestamp'] = 0; //time() - strtotime($version);
$output['duration'] = timestamp() - $timestamp;
$output['stoped'] = (get_variable('STOP_ALL_NETWORK') === 1 || get_variable('STOP_NTFM') === 1) && !$fake;
$output['count'] = $length;
$output['begTime'] = $moment;
$output['content'] = $without_content ? null : $content;
$output['history'] = ($full || $with_history) ? $history : null;
$output['tags'] = ($full || $with_tags) ? $tags : null;
$output['airports'] = ($full || $with_tags || $with_rates || $with_details) ? $airports : null;
$output['rates'] = ($full || $with_rates) ? $rates : null;
$output['details'] = ($full || $with_details) ? $details : null;
// $output['sql'] = $sql;
$output['raw'] = $with_raw ? $raw : null;
//
$output['limits'] = $limits;

// add @2024-08-31
$output['timestamp'] = timestamp();

output($output);
